package tech.aistar.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import tech.aistar.pojo.Card;
import tech.aistar.pojo.CardProduct;
import tech.aistar.pojo.Product;
import tech.aistar.pojo.User;
import tech.aistar.service.CardProductService;
import tech.aistar.service.CardService;
import tech.aistar.service.ProductService;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/cart")
public class CardController {


    @Autowired
    private CardService cardService;
    @Autowired
    private CardProductService cardProductService;
    @Autowired
    private ProductService productService;

    @RequestMapping("/add")
    public String add(Integer productId, @RequestParam(value = "num",defaultValue = "1") Integer num, HttpServletRequest request, Model model) {
        // 获取用户的购物车
        Card card = cardService.getOne(new QueryWrapper<Card>().lambda().eq(Card::getUserId, ((User) (request.getSession().getAttribute("userSession"))).getId()));
        // 构建购物车产品关联信息
        CardProduct cardProduct = new CardProduct();
        cardProduct.setCardId(card.getId());
        cardProduct.setProductId(productId);
        cardProduct.setNum(num);
        cardProductService.save(cardProduct);
        List<CardProduct> cardProductList = cardProductService.list(new QueryWrapper<CardProduct>().lambda().eq(CardProduct::getCardId, card.getId()));
        List<Product> list = new ArrayList<>();
        for (CardProduct cardProduct1 : cardProductList) {
            Integer productId1 = cardProduct1.getProductId();
            Product product = productService.getById(productId1);
            product.setNum(cardProduct1.getNum());
            list.add(product);
        }
        model.addAttribute("cardList",list );
        return "cart";
    }

    @RequestMapping("/list")
    public String add(HttpServletRequest request, Model model) {
        // 获取用户的购物车
        Card card = cardService.getOne(new QueryWrapper<Card>().lambda().eq(Card::getUserId, ((User) (request.getSession().getAttribute("userSession"))).getId()));
        List<CardProduct> cardProductList = cardProductService.list(new QueryWrapper<CardProduct>().lambda().eq(CardProduct::getCardId, card.getId()));
        List<Product> list = new ArrayList<>();
        for (CardProduct cardProduct1 : cardProductList) {
            Integer productId1 = cardProduct1.getProductId();
            Product product = productService.getById(productId1);
            list.add(product);
        }
        model.addAttribute("cardList",list );
        return "cart";
    }

    @RequestMapping("/clearAll")
    public String clearAll(HttpServletRequest request, Model model) {
        // 获取用户的购物车
        Card card = cardService.getOne(new QueryWrapper<Card>().lambda().eq(Card::getUserId, ((User) (request.getSession().getAttribute("userSession"))).getId()));
        // 清空购物车
        cardProductService.remove(new QueryWrapper<CardProduct>().lambda().eq(CardProduct::getCardId, card.getId()));
        List<Product> list = new ArrayList<>();
        model.addAttribute("cardList",list );
        return "cart";
    }

    @RequestMapping("/del")
    public String del(Integer id,HttpServletRequest request, Model model) {
        // 获取用户的购物车
        Card card = cardService.getOne(new QueryWrapper<Card>().lambda().eq(Card::getUserId, ((User) (request.getSession().getAttribute("userSession"))).getId()));
        // 删除购物车商品
        cardProductService.remove(new QueryWrapper<CardProduct>().lambda().eq(CardProduct::getProductId, id).eq(CardProduct::getCardId,card.getId() ));
        List<Product> list = new ArrayList<>();
        List<CardProduct> cardProductList = cardProductService.list(new QueryWrapper<CardProduct>().lambda().eq(CardProduct::getCardId, card.getId()));
        for (CardProduct cardProduct1 : cardProductList) {
            Integer productId1 = cardProduct1.getProductId();
            Product product = productService.getById(productId1);
            product.setNum(cardProduct1.getNum());
            list.add(product);
        }
        model.addAttribute("cardList",list );
        return "cart";
    }


    @RequestMapping("/checkout")
    public String checkout(HttpServletRequest request, Model model) {
        // 获取用户的购物车
        Card card = cardService.getOne(new QueryWrapper<Card>().lambda().eq(Card::getUserId, ((User) (request.getSession().getAttribute("userSession"))).getId()));
        List<Product> list = new ArrayList<>();
        List<CardProduct> cardProductList = cardProductService.list(new QueryWrapper<CardProduct>().lambda().eq(CardProduct::getCardId, card.getId()));
        for (CardProduct cardProduct1 : cardProductList) {
            Integer productId1 = cardProduct1.getProductId();
            Product product = productService.getById(productId1);
            product.setNum(cardProduct1.getNum());
            list.add(product);
        }

        model.addAttribute("totalMoney",list.stream().map(Product::getSalePrice).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).setScale(2,BigDecimal.ROUND_HALF_DOWN ).toString() );
        model.addAttribute("cardList",list );
        return "checkout";
    }



}

